package com.netsdk.lht2.rtsp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RTSPChecker {
    private static Map<String,Process> map = new HashMap<>();
    private static ExecutorService executorService = Executors.newFixedThreadPool(12);
    private static List<CommandTask> commandTaskList = new ArrayList<>();
//    public static boolean checkRTSP(String rtspUrl) {
//        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
//        try {
//            grabber.start();
//            return grabber.getFormat() != null;
//        }catch (Exception e){
//            return false;
//        }finally {
//            try {
//                grabber.stop();
//                grabber.release();
//            }catch (Exception e){}
//        }
//    }

    private static void init() throws Exception {
        String[] deviceIds = new String[]{
                // 陈家山路-胜辛路-东侧-西HG
                "31011410001321044006",
                // 胜辛路-陈家山路-南侧2-北HG
                "31011410001321044005",
                // 陈家山路-胜辛路-西侧-东HG
                "31011410001321044003",
                // 胜辛路-陈家山路-北侧2-南HG
                "31011410001321044002",
                // 平城路-城北路-东侧-西HG
                "31011410001321046005",
                // 城北路-平成路-南侧-北HG
                "31011410001321046004",
                 // 平城路-城北路-西侧-东HG
                "31011410001321046003",
                 // 城北路-平成路-北侧-南
                "31011410001321046002"
        };
        commandTaskList.add(new CommandTask("31011410001321044006","陈家山路-胜辛路-东侧-西HG"));
        commandTaskList.add(new CommandTask("31011410001321044005","胜辛路-陈家山路-南侧2-北HG"));
        commandTaskList.add(new CommandTask("31011410001321044003","陈家山路-胜辛路-西侧-东HG"));
        commandTaskList.add(new CommandTask("31011410001321044002","胜辛路-陈家山路-北侧2-南HG"));
        commandTaskList.add(new CommandTask("31011410001321046005","平城路-城北路-东侧-西HG"));
        commandTaskList.add(new CommandTask("31011410001321046004","城北路-平成路-南侧-北HG"));
        commandTaskList.add(new CommandTask("31011410001321046003","平城路-城北路-西侧-东HG"));
        commandTaskList.add(new CommandTask("31011410001321046002","城北路-平成路-北侧-南"));
        for (CommandTask commandTask : commandTaskList) {
            executorService.submit(commandTask);
            Thread.sleep(5000);
        }
    }

    public static void main(String[] args) throws Exception {
        init();
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                for (CommandTask commandTask : commandTaskList) {
                    try {
                        commandTask.getProcess().destroy();
                    }catch (Exception e){
                    }
                }
            }
        }));
        Thread.currentThread().join();
    }
}
